package com.zhu.androidsample.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * SharedPreferences的封装类。
 * 约定大于配置的原则，
 * 约定有几个xml，就有几个getXxxxx()方法
 * Created by ZCP on 2015/11/17.
 * mail:43426073@qq.com
 */
public class SharedPreferencesUtil {

    public static final String IS_FIRST = "isFirst";
    /**
     * 用户设置
     */
    private static final String FILE_SETTING = "setting";
    /**
     * 用户信息
     */
    private static final String FILE_USER = "user";
    private static Context context;

    public static void initSharedPreferencesUtil(Context mContext) {
        context = mContext;
    }

    public static SharedPreferences.Editor getSettingEditor() {
        if (context == null) {
            throw new RuntimeException("initSharedPreferencesUtil() method must be called before use!");
        }
        SharedPreferences sp = context.getSharedPreferences(FILE_SETTING, Context.MODE_PRIVATE);
        return sp.edit();
    }

    public static SharedPreferences getSetting() {
        if (context == null) {
            throw new RuntimeException("initSharedPreferencesUtil() method must be called before use!");
        }
        return context.getSharedPreferences(FILE_SETTING, Context.MODE_PRIVATE);
    }

    public static SharedPreferences.Editor getUserEditor() {
        if (context == null) {
            throw new RuntimeException("initSharedPreferencesUtil() method must be called before use!");
        }
        SharedPreferences sp = context.getSharedPreferences(FILE_USER, Context.MODE_PRIVATE);
        return sp.edit();
    }

    public static SharedPreferences getUser() {
        if (context == null) {
            throw new RuntimeException("initSharedPreferencesUtil() method must be called before use!");
        }
        return context.getSharedPreferences(FILE_USER, Context.MODE_PRIVATE);
    }


}
